/*
 * TepcoMeter:TepcoMetterWidgetService.java
 * Copyright © 2011 ho9ho9 All Rights Reserved.
 */
package com.ho9ho9.tepcometer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import com.ho9ho9.tepcometer.dto.WattDto;
import com.ho9ho9.tepcometer.logic.TepcoMetterLogic;

/**
 * TepcoMetterWidgetServiceクラス.
 * @author ho9ho9
 */
public class TepcoMetterWidgetService extends Service {

	private static final String TAG_NAME = "TepcoMetterWidgetService";
	private static final DateFormat FORMAT_UPDATED = new SimpleDateFormat("MM/dd HH:mm");
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.d(TAG_NAME, "startId:" + startId);
		// 既存処理呼び出し
		super.onStart(intent, startId);
		
		// Widget情報取得
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
		
		// 当日の消費電力を取得
		TepcoMetterLogic logic = new TepcoMetterLogic();
		WattDto wattDto = logic.getToDayWattInfo(this);
		if (wattDto != null) {
			// 取得結果を反映
			reflectViews(views, wattDto);
			// Widgetを更新
			manager.updateAppWidget(new ComponentName(this, TepcoMetterWidgetProvider.class), views);
		} else {
			// 当日の消費電力を取得できなかった場合
			Log.e(TAG_NAME, "use watt data failed!");
		}
		
		Log.d(TAG_NAME, "service end");
	}

	private void reflectViews(RemoteViews views, WattDto wattDto) {
		TepcoMetterLogic logic = new TepcoMetterLogic();
		// 結果から値を設定
		Integer currentWatt = wattDto.getCurrentHour().getWattValue();
		Integer calcRet = logic.calculateUseWatt(currentWatt, wattDto.getCapability());
		views.setTextViewText(R.id.useWatt, calcRet.toString());
		views.setTextViewText(R.id.updateDate, FORMAT_UPDATED.format(wattDto.getUpdated()));
		views.setTextViewText(R.id.currentWatt, currentWatt.toString());
		views.setTextViewText(R.id.maxWatt, wattDto.getCapability().toString());

		// グラフ描画
		drawWidgetGraph(views, calcRet);
	}

	private void drawWidgetGraph(RemoteViews views, Integer calcRet) {
		TepcoMetterLogic logic = new TepcoMetterLogic();
		// グラフ描画
		Bitmap mainParts = BitmapFactory.decodeResource(getResources(), R.drawable.w_left_parts_main);
		Bitmap bitmap = Bitmap.createBitmap(mainParts.getWidth(), mainParts.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		Paint paint = new Paint();
		LinearGradient gr = new LinearGradient(
				0, calculateGraphOffset(mainParts.getHeight(), calcRet), 
				0, mainParts.getHeight(),
				logic.getWattMetterGradientColor(calcRet), null, TileMode.CLAMP);
		paint.setShader(gr);
		canvas.drawRect(0, calculateGraphOffset(mainParts.getHeight(), calcRet), mainParts.getWidth(), mainParts.getHeight(), paint);
		views.setImageViewBitmap(R.id.graphCanvasView, bitmap);
	}


	private float calculateGraphOffset(int height, Integer calcRet) {
		// 開始位置のため100（MAX）から現在の利用率を引いたものに1%分の積を求める
		return (new Float(height) / 100) * (100 - calcRet);
	}

}
